package com.txsj.scan.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.txsj.scan.R;
import com.txsj.scan.bean.GoodsInfo;
import com.txsj.scan.utils.Trace;
import com.txsj.scan.utils.glide.GlideImgManager;

import java.util.ArrayList;
import java.util.List;

/**
 * @Created by WangChuangJun on 2018/12/5
 * @Describe 已选中的商品列表进行改变操作Adapter
 */
public class ChangeSelectedGoodsAdapter extends RecyclerView.Adapter<ChangeSelectedGoodsAdapter.ChangeSelectedViewHolder> {

    private List<GoodsInfo> list;

    private Context context;
    private OnItemCheckListener listener;
    public ChangeSelectedGoodsAdapter(Context context) {
        this.context = context;
        list = new ArrayList<>();
    }

    public void setList(List<GoodsInfo> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public List<GoodsInfo> getList() {
        return list;
    }

    /**
     * 获得当前list已经选中的个数
     * @return
     */
    public List<GoodsInfo> getSelectedList(){
        List<GoodsInfo> goodsInfos = new ArrayList<>();
        for (GoodsInfo goodsInfo:list){
            if (goodsInfo.isSelected()){
                goodsInfos.add(goodsInfo);
            }
        }
        return goodsInfos;
    }

    /**
     * 获取单条数据
     * @param position
     * @return
     */
    public GoodsInfo get(int position){
        return list.get(position);
    }

    /**
     * 对数据进行取消全选
     */
    public void clearSelect(){
        for (GoodsInfo goodsInfo:list){
            goodsInfo.setSelected(false);
        }
        notifyDataSetChanged();
    }

    /**
     * 更新列表是否被选中
     * @param position 改变的位置
     */
    public void updateRightData(int position) {
        boolean isSelect = !list.get(position).isSelected();
        list.get(position).setSelected(isSelect);
        notifyItemChanged(position);
    }

    @Override
    public ChangeSelectedViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        return new ChangeSelectedViewHolder(LayoutInflater.from(context).inflate(R.layout.item_change_selected_goods,parent,false));
    }

    @Override
    public void onBindViewHolder(ChangeSelectedViewHolder holder, final int position) {
        try {
            GoodsInfo goodsData = list.get(position);
            holder.tv_goods_name.setText(goodsData.getGoods_name() + "");
            GlideImgManager.getLoadImg(context, goodsData.getGoods_img(), holder.iv_goods_img);
            holder.tv_repertory.setText("库存：" + goodsData.getRepertory());
            if (goodsData.isSelected()) {
                holder.box_check.setBackgroundResource(R.drawable.box_select_s);
            } else {
                holder.box_check.setBackgroundResource(R.drawable.box_select_n);
            }
            holder.box_check.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (null != listener)listener.onCheckChange(position);
                }
            });
        } catch (Exception e) {
            Trace.e("异常=" + e);
        }
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public class ChangeSelectedViewHolder extends RecyclerView.ViewHolder{
        TextView tv_goods_name;//商品名称
        TextView tv_repertory;//库存
        ImageView iv_goods_img;//商品图片
        ImageView box_check; //是否选中
        public ChangeSelectedViewHolder(View itemView) {
            super(itemView);
            tv_goods_name = (TextView) itemView.findViewById(R.id.tv_goods_name);
            tv_repertory = (TextView) itemView.findViewById(R.id.tv_repertory);
            box_check = (ImageView) itemView.findViewById(R.id.box_check);
            iv_goods_img = (ImageView) itemView.findViewById(R.id.iv_goods_img);
        }
    }

    public void setListener(OnItemCheckListener listener) {
        this.listener = listener;
    }

    public interface OnItemCheckListener{
        void onCheckChange(int position);
    }
}
